﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) www.51mvc.com Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.51mvc.com/
 * Create By 小鱼(Leven)  - http://leven.com.cn
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Infancy.Core.Reflection;

namespace Infancy.Core.Entities {
    /// <summary>
    /// 对基本接口的实现,大部分实体直接或者间接集成本类,缓存封装获取属性操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public abstract class EntityBase<T> : IEntity where T : IEntity {
        private static Dictionary<Type, FastPropertyInfo[]> cache;
        private static object lockObject = new object();

        static EntityBase() {
            cache = new Dictionary<Type, FastPropertyInfo[]>();
        }

        #region IEntity Members

        /// <summary>
        /// 获取实体所有属性元数据,该操作已经缓存
        /// </summary>
        /// <returns></returns>
        public virtual FastPropertyInfo[] GetProperties() {
            FastPropertyInfo[] result = null;
            if (!cache.TryGetValue(typeof(T), out result)) {
                lock (lockObject) {
                    result = typeof(T).GetFastProperties();
                    cache.Add(typeof(T), result);
                }
            }
            return result;
        }

        #endregion

        #region Basic IEntity Members

        FastPropertyInfo[] IEntity.GetProperties() {
            return GetProperties();
        }

        #endregion
    }
}
